﻿
namespace D.Domain.SystemDomain.SysOrgDomain
{
    /// <summary>
    /// 聚合根
    /// </summary>
    public class SysOrgAggregate : AggregateRoot
    {
        private readonly List<SysOrgEntity> sysOrgEntities = new List<SysOrgEntity>();

        public void Add(SysOrgEntity sysOrgEntity)
        {
            var user = new SysOrgEntity(sysOrgEntity.Name, sysOrgEntity.Descption, sysOrgEntity.Location);
            sysOrgEntities.Add(user);

            AddEvent(new SysOrgAddedEvent(user));
        }
      
        public void Update()
        {
            var sysOrgEntity = sysOrgEntities.FirstOrDefault(u => u.Id == Id);

            if (sysOrgEntity == null)
            {
                throw new ArgumentException("User not found.");
            }

            sysOrgEntity.Update(sysOrgEntity.Name);

            AddEvent(new SysOrgUpdatedEvent(sysOrgEntity));
        }

        public void Delete(Guid userId)
        {
            var user = sysOrgEntities.FirstOrDefault(u => u.Id == userId);

            if (user == null)
            {
                throw new ArgumentException("User not found.");
            }

            sysOrgEntities.Remove(user);

            AddEvent(new SysOrgDeletedEvent(userId));
        }

        public IReadOnlyList<SysOrgEntity> GetUsers()
        {
            return sysOrgEntities.AsReadOnly();
        }
    }
}
